//
//  FLLFoodMoreViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/12.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLFoodMoreViewController.h"
#import "ZWYNetTool.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "FLLRecommendTableViewCell.h"
#import "FLLFoodTableViewCell.h"
#import "FLLMacro.h"
#import "FLLFoodMoreModel.h"
#import "FoodMoreSecData.h"
#import "FoodMoreData.h"
#import "UIImageView+WebCache.h"
#import "FLLFoodWebVC.h"

@interface FLLFoodMoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *foodMoreTableView;
@property (nonatomic, strong) FLLFoodMoreModel *fllFoodMoreModel;
@property (nonatomic, strong) NSMutableArray<FoodMoreSecData *> *moreTableArray;
@property (nonatomic, assign) BOOL isRefresh;
@property (nonatomic, assign) NSInteger pageCount;
@end

@implementation FLLFoodMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createPOSTRequest];
    [self createFoodMoreTableView];
}
- (void)createPOSTRequest
{

    
   
    NSString *bodyString = [NSString stringWithFormat:@"page=%zd&methodName=TopicList&size=20&version=4.40&", _pageCount];
    [ZWYNetTool POST:@"http://api.izhangchu.com/?appVersion=4.5&sysVersion=9.0.2&devModel=iPhone" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        
        // 加载
        if (_isRefresh == YES) {
            _pageCount++;
            
        }
        // 刷新
        else
        {
            [self.moreTableArray removeAllObjects];
            self.pageCount = 1;
        }
      
        self.fllFoodMoreModel = [FLLFoodMoreModel modelWithDic:result];

        // 加载
        if (_isRefresh == YES) {
            [self.moreTableArray addObjectsFromArray:(NSMutableArray *)self.fllFoodMoreModel.data.data];
        }
        // 刷新
        else
        {
            self.moreTableArray = (NSMutableArray *)self.fllFoodMoreModel.data.data;
        }

        [self.foodMoreTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)createFoodMoreTableView
{

    self.foodMoreTableView = [[UITableView alloc] init];
    self.foodMoreTableView.delegate = self;
    self.foodMoreTableView.dataSource = self;
    [self.view addSubview:self.foodMoreTableView];
    [self.foodMoreTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    
    // 注册
    [self.foodMoreTableView registerNib:[UINib nibWithNibName:@"FLLFoodTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"foodTableCell"];
    
    
    // 下拉刷新
    self.foodMoreTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = NO;
        [self createPOSTRequest];
        [self.foodMoreTableView.mj_header endRefreshing];
    }];
    
    // 上拉加载
    self.foodMoreTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        self.isRefresh = YES;
        [self createPOSTRequest];
        [self.foodMoreTableView.mj_footer endRefreshing];
        
    }];

    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _moreTableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLFoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodTableCell"];
    [cell.topImageView sd_setImageWithURL:[NSURL URLWithString:_moreTableArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
    }];
    cell.titleLabel.text = _moreTableArray[indexPath.row].title;
    cell.titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
    cell.contentLabel.text = _moreTableArray[indexPath.row].description_Data;
    cell.contentLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
   
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 200 * kHeightScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *idString = _moreTableArray[indexPath.row].id_Data;
    FLLFoodWebVC *webVC = [[FLLFoodWebVC alloc] init];
    webVC.linkString = idString;
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
